<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/system/scheduler/jobList' }">定时任务列表</el-breadcrumb-item>
      <el-breadcrumb-item>定时任务信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <div class="autumn-center">
      <div class="edit-form">
        <el-form ref="editFormRef" size="large" label-width="90px" :model="editForm" :rules="editFormRules">
          <el-row>
            <el-col :span="12">
              <el-form-item label="标识" prop="name">
                <el-input v-model="editForm.name" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="任务组" prop="groupCode">
                <el-select v-model="editForm.groupCode" placeholder="请选择">
                  <el-option v-for="(value, key, index) in $listDict('TASK_GROUP')" :key="index" :label="value"
                    :value="key">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="Cron" prop="cron">
                <el-input v-model="editForm.cron"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="执行类" prop="className">
                <el-input v-model="editForm.className" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" resize="none" v-model="editForm.remark" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <div class="form-foot-button">
          <el-button type="primary" @click="submit(editFormRef)">保存</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { FormInstance } from 'element-plus'
import { viewScheduler, saveScheduler, updateScheduler } from 'src/api/system/scheduler'
import { $listDict } from 'src/hooks/useCommon'

const route = useRoute()
const router = useRouter()

const editFormRef = ref<FormInstance>()
const editForm = reactive({
  id: '',
  name: '',
  groupCode: '',
  cron: '',
  className: '',
  remark: ''
})
const editFormRules = {
  name: [
    { required: true, message: '请输入任务标识', trigger: 'blur' }
  ],
  groupCode: [
    { required: true, message: '请选择任务组', trigger: 'blur' }
  ],
  cron: [
    { required: true, message: '请输入执行表达式', trigger: 'change' }
  ],
  className: [
    { required: true, message: '请输入执行类', trigger: 'blur' }
  ]
}

// 填充表单数据
if (route.query.id) {
  const id = route.query.id as string
  viewScheduler(id).then(({ data }) => {
    Object.assign(editForm, data)
  })
}

// 提交
const submit = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(valid => {
    if (!valid) return
    if (!editForm.id) {
      saveScheduler(editForm).then(data => {
        cancel()
      })
    } else {
      updateScheduler(editForm).then(data => {
        cancel()
      })
    }
  })
}

// 取消
const cancel = () => {
  router.push('/system/scheduler/jobList')
}
</script>

<style lang="scss" scoped>
</style>
